<?php

namespace app\web\controller;
use app\web\model\LineGoodsModel;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 套餐管理
 */
class LinePackageController extends StoreBaseController
{
 	protected $model; 	
	protected $goodstypemodel;
	
    protected function _initialize() {
        parent::_initialize();
		$this->model = new LineGoodsModel();
    }
     /**
     * 套餐管理
     * @author [lx] 
     */
    public function index( $page = 1){
    	if(request()->isPost()){
		 	$param = input('post.');
			//套餐名称
			if($param['goods_name']){
				$where['goods_name'] = ['like', '%'.$param['goods_name'].'%'];
			}	 				
	 	}
		
		$where['is_pk'] =1;
		$where['store_id']=session('storeInfo')['id'];
		$list = db("line_goods")->where($where)->paginate(20);
        return $this->fetch('index', ['list' => $list]);
    }


    /**
     * 
     * 添加
     * @author [lx] 
     */
    public function add()
    {	
        if(request()->isAjax()){
        	$param=input('post.');
			$param['is_pk']=1;
			if(input()['goodsid1']){
				$goodsid=$param['goodsid1'];
				$goods_names=$param['goods_names1'];
				$param['goodsid']=implode($goodsid, ",");
				$param['goods_names']=implode($goods_names, ",");
				$param['store_id']	=session('storeInfo')['id'];
				$nums1=$param['num1'];
				foreach($goods_names as $k=>$v){
					$goods_names[$k]=$goods_names[$k]."*".$nums1[$k];
				}
				$param['nums1']=implode($param['num1'], ",");
			}
			if(input()["goodsid2"]){
				$goodsid=$param['goodsid2'];
				$goods_names=$param['goods_names2'];
				$param['goods_zs']=implode($goodsid, ",");
				$param['goods_znames']=implode($goods_names, ",");
				$nums2=$param['num2'];
				foreach($goods_names as $k=>$v){
				 $goods_names[$k]=$goods_names[$k]."*".$nums2[$k];
				}
				$param['nums2']=implode($param['num2'], ",");
			}
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('index'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }


    /**
     * [edit]
     * @return [type] [description]
     * @author [lx]
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');  
			if(isset($param['goodsid1'])){
				$goodsid=$param['goodsid'];
				$goods_names=$param['goods_names'];
				$nums=$param['num'];
				foreach($goods_names as $k=>$v){
					$goods_names[$k]=$goods_names[$k]."*".$nums[$k];
				}
				$param['nums']=implode($param['num'], ",");
				$param['goodsid']=implode($goodsid, ",");
				$param['goods_names']=implode($goods_names, ","); 
			}    
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('line_package/index'));
            } else {
                $this->error('修改失败');
            }
        }
		$id = input('param.id');
		$packageinfo = $this->model->find($id);
        $where['id'] = array('in',$packageinfo['goodsid']);
		$goods_list=Db::name("line_goods")->where($where)->order("add_time desc")->select();
		$this->assign('goods_list',$goods_list);
		$this->assign('model',$packageinfo);
        return $this->fetch();
    }


    /**
     * 商品管理
     * @author [lx] 
     */
    public function goods_list( ){
    	$where['store_id']=session('storeInfo')['id'];
    	if(request()->isPost()){
		 	$param = input('post.');
			if($param['goods_name']){
				$where['goods_name'] = ['like', '%'.$param['goods_name'].'%'];
			}			
	 	}	
		$this->assign('type',input("type"));	
		$where['is_pk']=0;
		$list=Db::name("line_goods")->where($where)->select();
        return $this->fetch('goods_list', ['list' => $list]);
    }
	/**
	 * 删除套装商品
	 */
	public function del_goods(){
		$pid=input("pid");
		$gid=input("gid");
		$pack=db("line_package")->find($pid);
		$ids=explode(",",$pack['goodsid']);
		$names=explode(",",$pack['goods_names']);
		$index=array_search($gid,$ids);
		unset($ids[$index]);
		unset($names[$index]);
		$update['goodsid']=implode(",",$ids);
		$update['goods_names']=implode(",",$names);
		$res=db("line_package")->where("id",$pid)->update($update);
		if($res){
			 return json(['code'=>200,'msg'=>'删除成功']);
		}else{
			 return json(['code'=>406,'msg'=>'删除失败']);
		}
	}
    /**
     * [del删除]
     * @author [lx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->goodsmodel->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}